/**
 * @file    diag_curr.c
 * @brief   电流类故障诊断
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-19
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-19 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */
/* includes ------------------------------------------------------------------*/
#include "diag_curr.h"
#include "diagnosis.h"
#include "param_mng.h"
#include "var_mng.h"
#include "event_mng.h"
#include "cfg_prj.h"
#include "sample.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/
/* static variables ----------------------------------------------------------*/
static diag_data_type diag_occ_data = {0};
static diag_data_type diag_occ2_data = {0};
static diag_data_type diag_ocd1_data = {0};
static diag_data_type diag_ocd2_data = {0};
/* global variables ----------------------------------------------------------*/
extern uint8_t wifi_send_flag;
/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  充电过流诊断
 * @param  cycle : 周期
 * @param  smp_val :采样值
 */
void diag_curr_occ(uint16_t cycle)
{
    diag_para_type diag_occ1_para = {0};
	diag_para_type diag_occ2_para = {0};
	float smp_val = 0;
	uint16_t occ_times = 0;
	
	smp_val = sample_get_curr();//var_get_data(VAR_ID_BUS_CURR) * VAR_CURR_FACTOR - VAR_CURR_OFFSET;
	uint8_t sys_state = var_get_data(VAR_ID_CURR_DR);
	
    diag_occ1_para.alm_param = param_get_data(PAR_ID_OCC_ALM_VAL) * 0.1;
    diag_occ1_para.alm_recv = param_get_data(PAR_ID_OCC_ALM_RCV) * 0.1;
    diag_occ1_para.alm_dly = param_get_data(PAR_ID_OCC_ALM_DLY);
    
    diag_occ1_para.prp_param = param_get_data(PAR_ID_OCC_PRP_VAL) * 0.1;
    diag_occ1_para.prp_dly = param_get_data(PAR_ID_OCC_PRP_DLY);
    diag_occ1_para.prp_recv = param_get_data(PAR_ID_OCC_RCV_DLY) * 1000;
    diag_occ1_para.lock_times = param_get_data(PAR_ID_OCC_LOCK_TIMES);
	
	diag_occ2_para.prp_param = param_get_data(PAR_ID_OCC2_PRP_VAL) * 0.1;
    diag_occ2_para.prp_dly = param_get_data(PAR_ID_OCC2_PRP_DLY);
    diag_occ2_para.prp_recv = param_get_data(PAR_ID_OCC2_RCV_DLY) * 1000;
    diag_occ2_para.lock_times = param_get_data(PAR_ID_OCC2_LOCK_TIMES);
    /* 告警诊断*/
    if(0 == diag_occ_data.alm_state)
    {
        if(smp_val >= diag_occ1_para.alm_param)
        {
            diag_occ_data.alm_time_cnt += cycle;
            if(diag_occ_data.alm_time_cnt >= diag_occ1_para.alm_dly) 
            {
                diag_occ_data.alm_state = 1;
				event_add_to_sram(EVT_ID_ALM_OCC , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_OCC_ALM_VAL));
                diag_occ_data.alm_time_cnt = 0;
				diag_occ_data.alm_recv_cnt = 0;
				wifi_send_flag = 1;
            }   
        }
        else
        {
            diag_occ_data.alm_time_cnt = 0;
        }
    }
    else
    {
		// 充电过流必须放电解除，且电流必须大于某个值才能释放，防止逆变器带载自动恢复 
        if((CURR_DR_DSG == sys_state) && (-smp_val >= CHG_RECV_CURR))
        {
            diag_occ_data.alm_state = 0;
            diag_occ_data.alm_time_cnt = 0;
			diag_occ_data.alm_recv_cnt = 0;
        }
		else
		{
			// 定时解除告警
			diag_occ_data.alm_recv_cnt += cycle;
            if(diag_occ_data.alm_recv_cnt >= diag_occ1_para.prp_recv)
            {
                diag_occ_data.alm_state = 0;
				diag_occ_data.alm_time_cnt = 0;
                diag_occ_data.alm_recv_cnt = 0;
            }
		}
    }
    diag_set_alarm(ALM_ID_OCC , diag_occ_data.alm_state);
    /* 保护诊断 */
    if((0 == diag_occ_data.prp_state) && (0 == diag_occ2_data.prp_state))
    {
		if(smp_val >= diag_occ2_para.prp_param)
        {
            diag_occ_data.prp_time_cnt += cycle;
            diag_occ2_data.prp_time_cnt += cycle;
            if(diag_occ2_data.prp_time_cnt >= diag_occ2_para.prp_dly) 
            {
				event_add_to_sram(EVT_ID_PRP_OCC2 , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_OCC2_PRP_VAL));
				diag_occ2_data.prp_state = 1;
     			diag_occ2_data.prp_time_cnt = 0;
				occ_times = param_get_data(PAR_ID_OCC_PRP_TIMES) + 1;
				param_set_data(PAR_ID_OCC_PRP_TIMES , occ_times);							
				if((diag_occ1_para.lock_times != 0)&&(diag_occ_data.prp_con_times == diag_occ1_para.lock_times))
				{				
					event_add_to_sram(EVT_ID_OCC_LOCK , diag_occ_data.prp_con_times, diag_occ1_para.lock_times);
				}
				wifi_send_flag = 1;
            }  
            else if(diag_occ_data.prp_time_cnt >= diag_occ1_para.prp_dly)
            {
				event_add_to_sram(EVT_ID_PRP_OCC , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_OCC_PRP_VAL));
				diag_occ_data.prp_state = 1;
                diag_occ_data.prp_time_cnt = 0;			
				occ_times = param_get_data(PAR_ID_OCC_PRP_TIMES) + 1;
				param_set_data(PAR_ID_OCC_PRP_TIMES , occ_times);
				if((diag_occ1_para.lock_times != 0)&&(diag_occ_data.prp_con_times == diag_occ1_para.lock_times))
				{
					event_add_to_sram(EVT_ID_OCC_LOCK , diag_occ_data.prp_con_times, diag_occ1_para.lock_times);
				}
				wifi_send_flag = 1;
            } 
            else
            {
                
            }
        }
		else if(smp_val >= diag_occ1_para.prp_param)
		{
			diag_occ2_data.prp_time_cnt = 0;
			diag_occ_data.prp_time_cnt += cycle;
			if(diag_occ_data.prp_time_cnt >= diag_occ1_para.prp_dly)
			{
				event_add_to_sram(EVT_ID_PRP_OCC , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_OCC_PRP_VAL));
				diag_occ_data.prp_state = 1;
                diag_occ_data.prp_time_cnt = 0;	
				occ_times = param_get_data(PAR_ID_OCC_PRP_TIMES) + 1;
				param_set_data(PAR_ID_OCC_PRP_TIMES , occ_times);
				if((diag_occ1_para.lock_times != 0)&&(diag_occ_data.prp_con_times == diag_occ1_para.lock_times))
				{
					event_add_to_sram(EVT_ID_OCC_LOCK , diag_occ_data.prp_con_times, diag_occ1_para.lock_times);
				}
				wifi_send_flag = 1;
			}
		}
		else
		{
			diag_occ_data.prp_time_cnt = 0;
			diag_occ2_data.prp_time_cnt = 0;
		}
    }
    else
    {
        if((CURR_DR_DSG == sys_state) && (-smp_val >= CHG_RECV_CURR))
        {
            diag_occ_data.prp_state = 0;
            diag_occ_data.prp_time_cnt = 0;
            diag_occ_data.prp_con_times = 0;
			diag_occ2_data.prp_state = 0;
            diag_occ2_data.prp_time_cnt = 0;
            diag_occ2_data.prp_con_times = 0;
			diag_occ_data.alm_recv_cnt = 0;
        }
        else
        {
            if(diag_occ_data.prp_con_times < diag_occ1_para.lock_times)
            {
                diag_occ_data.prp_recv_cnt += cycle;
                if(diag_occ_data.prp_recv_cnt >= diag_occ1_para.prp_recv)
                {
                    diag_occ_data.prp_recv_cnt = 0;
                    diag_occ_data.prp_state = 0;
					diag_occ2_data.prp_state = 0;
					++ diag_occ_data.prp_con_times;
					event_add_to_sram(EVT_ID_OCC_TRY , diag_occ_data.prp_con_times, diag_occ1_para.lock_times);
                }
            }
        }
    }

    diag_set_prp(PRP_ID_OCC , diag_occ_data.prp_state);
	
	diag_set_prp(PRP_ID_OCC2 , diag_occ2_data.prp_state);
}

/**
 * @brief  放电过流诊断
 * @param  cycle : 周期
 * @param  sys_state : 系统状态
 * @param  smp_val :采样值
 */
void diag_curr_ocd(uint16_t cycle)
{
    diag_para_type diag_ocd1_para = {0};
    diag_para_type diag_ocd2_para = {0};
	uint16_t ocd_times = 0;
	uint8_t sys_state = var_get_data(VAR_ID_CURR_DR);
	float smp_val = 0;//var_get_data(VAR_ID_BUS_CURR) * VAR_CURR_FACTOR - VAR_CURR_OFFSET;	
	/* 放电电流判断符号取反 */
	smp_val = -sample_get_curr();
    diag_ocd1_para.alm_param = param_get_data(PAR_ID_OCD1_ALM_VAL) * 0.1;
    diag_ocd1_para.alm_recv = param_get_data(PAR_ID_OCD1_ALM_RCV) * 0.1;
    diag_ocd1_para.alm_dly = param_get_data(PAR_ID_OCD1_ALM_DLY);
    
    diag_ocd1_para.prp_param = param_get_data(PAR_ID_OCD1_PRP_VAL) * 0.1;
    diag_ocd1_para.prp_dly = param_get_data(PAR_ID_OCD1_PRP_DLY);
    diag_ocd1_para.prp_recv = param_get_data(PAR_ID_OCD1_RCV_DLY) * 1000;
    diag_ocd1_para.lock_times = param_get_data(PAR_ID_OCD1_LOCK_TIMES);

    diag_ocd2_para.prp_param = param_get_data(PAR_ID_OCD2_PRP_VAL) * 0.1;
    diag_ocd2_para.prp_dly = param_get_data(PAR_ID_OCD2_PRP_DLY);
    diag_ocd2_para.prp_recv = param_get_data(PAR_ID_OCD1_RCV_DLY)* 1000;
    diag_ocd2_para.lock_times = param_get_data(PAR_ID_OCD2_LOCK_TIMES);
    /* 告警诊断*/
    if(0 == diag_ocd1_data.alm_state)
    {
        if(smp_val >= diag_ocd1_para.alm_param)
        {
            diag_ocd1_data.alm_time_cnt += cycle;
            if(diag_ocd1_data.alm_time_cnt >= diag_ocd1_para.alm_dly) 
            {
				event_add_to_sram(EVT_ID_ALM_OCD , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_OCD1_ALM_VAL));
                diag_ocd1_data.alm_state = 1;
                diag_ocd1_data.alm_time_cnt = 0;
				diag_ocd1_data.alm_recv_cnt = 0;
				wifi_send_flag = 1;
            }   
        }
        else
        {
            diag_ocd1_data.alm_time_cnt = 0;
        }
    }
    else
    {
		// 放电过流告警，必须充电电流大于某个值才解除
        if((CURR_DR_CHG == sys_state) && (-smp_val >= DSG_RECV_CURR))
        {
            diag_ocd1_data.alm_state = 0;
            diag_ocd1_data.alm_time_cnt = 0;
			diag_ocd1_data.alm_recv_cnt = 0;
        }
		else
		{
			// 定时解除告警
			diag_ocd1_data.alm_recv_cnt += cycle;
            if(diag_ocd1_data.alm_recv_cnt >= diag_ocd1_para.prp_recv)
            {
                diag_ocd1_data.alm_state = 0;
				diag_ocd1_data.alm_time_cnt = 0;
				diag_ocd1_data.alm_recv_cnt = 0;
            }
		}
    }
    diag_set_alarm(ALM_ID_OCD , diag_ocd1_data.alm_state);
    /* 保护诊断 */
    if((0 == diag_ocd1_data.prp_state) && (0 == diag_ocd2_data.prp_state))
    {
        if(smp_val >= diag_ocd2_para.prp_param)
        {
            diag_ocd1_data.prp_time_cnt += cycle;
            diag_ocd2_data.prp_time_cnt += cycle;
            if(diag_ocd2_data.prp_time_cnt >= diag_ocd2_para.prp_dly) 
            {
                diag_ocd2_data.prp_state = 1;
                diag_ocd2_data.prp_time_cnt = 0;
				ocd_times = param_get_data(PAR_ID_OCD_PRP_TIMES) + 1;
				param_set_data(PAR_ID_OCD_PRP_TIMES , ocd_times);							
				event_add_to_sram(EVT_ID_PRP_OCD2 , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_OCD2_PRP_VAL));
				if((0 != diag_ocd1_para.lock_times)&&(diag_ocd1_data.prp_con_times == diag_ocd1_para.lock_times))
				{
					event_add_to_sram(EVT_ID_OCD_LOCK,diag_ocd1_data.prp_con_times,diag_ocd1_para.lock_times);
				}
				wifi_send_flag = 1;
            }  
            else if(diag_ocd1_data.prp_time_cnt >= diag_ocd1_para.prp_dly)
            {
                diag_ocd1_data.prp_state = 1;
                diag_ocd1_data.prp_time_cnt = 0;
				ocd_times = param_get_data(PAR_ID_OCD_PRP_TIMES) + 1;
				param_set_data(PAR_ID_OCD_PRP_TIMES , ocd_times);
				event_add_to_sram(EVT_ID_PRP_OCD , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_OCD1_PRP_VAL));
				if((0 != diag_ocd1_para.lock_times)&&(diag_ocd1_data.prp_con_times == diag_ocd1_para.lock_times))
				{
					event_add_to_sram(EVT_ID_OCD_LOCK,diag_ocd1_data.prp_con_times,diag_ocd1_para.lock_times);
				}
				wifi_send_flag = 1;
            } 
            else
            {
                
            }
        }
        else if(smp_val >= diag_ocd1_para.prp_param)
        {
            diag_ocd2_data.prp_time_cnt = 0;
            diag_ocd1_data.prp_time_cnt += cycle;
            if(diag_ocd1_data.prp_time_cnt >= diag_ocd1_para.prp_dly)
            {
                diag_ocd1_data.prp_state = 1;
                diag_ocd1_data.prp_time_cnt = 0;
				ocd_times = param_get_data(PAR_ID_OCD_PRP_TIMES) + 1;
				param_set_data(PAR_ID_OCD_PRP_TIMES , ocd_times);
				event_add_to_sram(EVT_ID_PRP_OCD , var_get_data(VAR_ID_BUS_CURR), param_get_data(PAR_ID_OCD1_PRP_VAL));
				if((0 != diag_ocd1_para.lock_times)&&(diag_ocd1_data.prp_con_times == diag_ocd1_para.lock_times))
				{
					event_add_to_sram(EVT_ID_OCD_LOCK,diag_ocd1_data.prp_con_times,diag_ocd1_para.lock_times);
				}
            } 
        }
        else
        {
            diag_ocd1_data.prp_state = 0;
            diag_ocd1_data.prp_time_cnt = 0;
            diag_ocd2_data.prp_state = 0;
            diag_ocd2_data.prp_time_cnt = 0;
        }
    }
    else
    {
        if((CURR_DR_CHG == sys_state) && (-smp_val >= DSG_RECV_CURR))
        {
            diag_ocd1_data.prp_state = 0;
            diag_ocd1_data.prp_time_cnt = 0;
			diag_ocd1_data.prp_con_times = 0;
            diag_ocd2_data.prp_state = 0;
            diag_ocd2_data.prp_time_cnt = 0;
			diag_ocd2_data.prp_state = 0;
			diag_ocd1_data.prp_recv_cnt = 0;
        }
        else
        {
            if(diag_ocd1_data.prp_con_times < diag_ocd1_para.lock_times)
            {
                diag_ocd1_data.prp_recv_cnt += cycle;
                if(diag_ocd1_data.prp_recv_cnt >= diag_ocd1_para.prp_recv)
                {
                    diag_ocd1_data.prp_recv_cnt = 0;
                    diag_ocd1_data.prp_state = 0;
                    diag_ocd2_data.prp_state = 0;
					++ diag_ocd1_data.prp_con_times;
					event_add_to_sram(EVT_ID_OCD_TRY,diag_ocd1_data.prp_con_times,diag_ocd1_para.lock_times);
                }
            }
        }
    }

    diag_set_prp(PRP_ID_OCD , diag_ocd1_data.prp_state);

    diag_set_prp(PRP_ID_OCD2 , diag_ocd2_data.prp_state);
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


